import Vue from 'vue'
import request from '@/utils/request.js'
import setting from '@/settings'
import '@/utils/filters'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import 'windi.css'
import '@/styles/index.scss' // global css

/**
 * 单条记录的状态
 */
const ENABLED_VALUE = 1,
  DISABLED_VALUE = 0,
  STATUS_OPTIONS = {
    [ENABLED_VALUE]: '启用',
    [DISABLED_VALUE]: '停用'
  },
  STATUS_OPTIONS_ARRAY = Array.from(
    Object.entries(STATUS_OPTIONS),
    ([value, label]) => ({
      value: Number(value),
      label
    })
  )
export { STATUS_OPTIONS, STATUS_OPTIONS_ARRAY, ENABLED_VALUE, DISABLED_VALUE }

/**
 * 挂载dayjs
 */
import dayjs from 'dayjs'
import isBetween from 'dayjs/plugin/isBetween.js'
import isSameOrAfter from 'dayjs/plugin/isSameOrAfter.js'
import isSameOrBefore from 'dayjs/plugin/isSameOrBefore.js'
import isToday from 'dayjs/plugin/isToday.js'
import isTomorrow from 'dayjs/plugin/isTomorrow.js'
import isYesterday from 'dayjs/plugin/isYesterday.js'
import objectSupport from 'dayjs/plugin/objectSupport.js'
import relativeTime from 'dayjs/plugin/relativeTime.js'
import minMax from 'dayjs/plugin/minMax.js'
import customParseFormat from 'dayjs/plugin/customParseFormat.js'
import duration from 'dayjs/plugin/duration.js'
import toObject from 'dayjs/plugin/toObject.js'
dayjs.extend(isBetween)
dayjs.extend(isSameOrAfter)
dayjs.extend(isSameOrBefore)
dayjs.extend(isToday)
dayjs.extend(isTomorrow)
dayjs.extend(isYesterday)
dayjs.extend(objectSupport)
dayjs.extend(relativeTime)
dayjs.extend(minMax)
dayjs.extend(customParseFormat)
dayjs.extend(duration)
dayjs.extend(toObject)
Object.defineProperty(Vue.prototype, '$dayjs', {
  value: dayjs
})

/**
 * 挂载lodash
 */
import _ from 'lodash-es'
Object.defineProperty(Vue.prototype, '$lo', {
  value: _
})

/**
 * 挂载qs
 */
import qs from 'qs'
Object.defineProperty(Vue.prototype, '$qs', {
  value: qs
})

/**
 * 挂载常用工具函数
 */
import { isEmpty, notEmpty } from '@/utils'
Object.defineProperty(Vue.prototype, '$isEmpty', {
  value: isEmpty
})
Object.defineProperty(Vue.prototype, '$notEmpty', {
  value: notEmpty
})
// [译]d: denominator分子; n: numerator分母; o: operator运算符;
Vue.prototype.$operation = ( d, o = '/', n = 100) => {
  if (isEmpty(d)) {
    return ''
  } else {
    return eval(d + o + n)
  }
}


/**
 * 自适应列宽
 */
/*import AFTableColumn from 'af-table-column'
Vue.use(AFTableColumn)*/

/**
 * axios捷径
 */
import createAxiosShortcut from 'axios-shortcut'
const axiosShortcut = createAxiosShortcut(
  request // axios或axios实例
)
for (let k in axiosShortcut) {
  if (!Vue.prototype[`$${k}`]) {
    Object.defineProperty(Vue.prototype, `$${k}`, {
      value: axiosShortcut[k]
    })
  }
}

/**
 * 图片上传
 */
import 'imgpond/dist/style.css'
import Imgpond from 'imgpond'
Vue.use(Imgpond, {
  upload: (file, context) =>
    new Promise((resolve, reject) => {
      Vue.prototype
        .$POST.upload(
          import.meta.env.VITE_APP_UPLOAD_API + 'upload',
          {
            file,
            domainId: import.meta.env.VITE_APP_domainId,
            dir: 'img',
            ...context.$attrs.requestParam
          },
          {
            baseURL: '', // 针对prod环境中baseAPI为相对路径的情况
            timeout: 20000
          }
        )
        .then(res => {
          resolve(res.data)
        })
        .catch(e => {
          reject(e)
        })
    })
})

/**
 * 文件上传
 */
import Filepool from 'filepool'
import { onAbort, upload } from '@/utils/chunkUpload'
Vue.use(Filepool, {
  upload,
  onAbort,
  fileTypeCatalog: {
    video: {
      maxSize: 1024
    },
    audio: {
      accept: '.mp3,.wav',
      maxSize: 200
    },
    word: {
      accept: '.docx'
    },
    credentials: {},
    agencydocs: {}
  }
})

/**
 * 挂载全局设置参数
 */
import settings from '@/settings.js'
Object.defineProperty(Vue.prototype, '$settings', {
  value: settings
})

/**
 * 坐标拾取
 */
import 'coord-picker/dist/style.css'
import CoordPicker from 'coord-picker'
export const useCoordPicker = city => {
  window._AMapSecurityConfig = JSON.parse(import.meta.env.VITE_APP_AMapSecurityConfig)
  Vue.use(CoordPicker, {
    loadOptions: {
      key: import.meta.env.VITE_APP_AMapKey
    },
    city,
    addressComponent: {
      province: false,
      city: false,
      district: false
    }
  })
}

/**
 * 富文本
 */
import useMiniMCE from '@/components/MiniMCE'
useMiniMCE()

/**
 * 全局注册 ElementUI
 */
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// set ElementUI lang to EN
// Vue.use(ElementUI, { locale })
// 如果想要中文版 element-ui，按如下方式声明
Vue.use(ElementUI, {
  size: 'medium'
})

/**
 * 全局注册 element-verify
 */
import ElementVerify from 'element-verify'
Vue.use(ElementVerify)

/**
 * 时间范围选择
 */
import TimeRangePicker from 'time-range-picker'
Vue.use(TimeRangePicker)
// 默认日期
let defaultDateShort = {}
for (let k in TimeRangePicker.defaultDate) {
  defaultDateShort[k] = Array.isArray(TimeRangePicker.defaultDate[k])
    ? TimeRangePicker.defaultDate[k].map(v => v.substr(0, 10))
    : TimeRangePicker.defaultDate[k].substr(0, 10)
}
Vue.prototype.$defaultDate = TimeRangePicker.defaultDate
Vue.prototype.$defaultDateShort = defaultDateShort
const defaultDate = TimeRangePicker.defaultDate
export { defaultDate, defaultDateShort }

/**
 * 权限按钮
 */
import AuthButton from '@/components/AuthButton/index.js'
import { getPageBtnHash } from '@/permission'
Vue.use(AuthButton, {
  show (name) {
    return getPageBtnHash()[name]
  }
})

/**
 * 图片预览
 */
import 'pic-viewer/dist/style.css'
import PicViewer from 'pic-viewer'
Vue.use(PicViewer)

/**
 * 全局注册 kikimore
 */
import 'kikimore/dist/style.css'
import {
  FormDialog,
  PopButton,
  PopSwitch,
  Select,
} from 'kikimore'
;[
  {
    component: PopButton,
    config: {
      // size: 'mini',
      type: 'text'
    }
  },
  {
    component: PopSwitch,
    config: {
      'active-value': ENABLED_VALUE,
      'inactive-value': DISABLED_VALUE,
      'active-text': STATUS_OPTIONS[ENABLED_VALUE],
      'inactive-text': STATUS_OPTIONS[DISABLED_VALUE]
    }
  },
  {
    component: FormDialog,
    config: {
      'destroy-on-close': true
    }
  },
  {
    component: Select,
    config: {
      props: {
        value: 'dataValue',
        label: 'dataName'
      }
    }
  },
].map(({ component, config }) => Vue.use(component, config))

/**
 * 全局注册 sweetalert2-preset
 */
import 'sweetalert2-preset/dist/style.css'
import Swal from 'sweetalert2-preset'
Object.defineProperty(Vue.prototype, '$swal', {
  value: Swal
})

/**
 * 注册全局业务组件
 */
import globalComponents from '@/components-g'
Vue.use(globalComponents)




/**
 * 打印环境变量
 */
console.log('\n')
console.log(`%cimport.meta.env.MODE:`, 'color:#409EFF; font-weight:bold;', import.meta.env.MODE)

/**
 * 打印 Git 信息
 */
if (process.VERSION && process.COMMIT) {
  const separatorIndex = process.COMMIT.indexOf('|')
  console.log(`%cCommit Message:`, 'color:#409EFF; font-weight:bold;', process.COMMIT.substr(separatorIndex + 1))
  console.log(`%cCommit Time:`, 'color:#409EFF; font-weight:bold;', process.COMMIT.substr(0, separatorIndex))
  console.log(`%cCommit ID:`, 'color:#409EFF; font-weight:bold;', process.VERSION)
  console.log('\n')
}
